const express = require('express')
const mysql = require('mysql')
const constant = require('./const')
const cors = require('cors')
const voice = require('./voice')

const app = express()
app.use(cors())

app.get('/', (req, res) => {
	res.send(new Date().toDateString())
})

// 建立链接数据库的方法
function connect() {
	return mysql.createConnection({
		host: 'localhost',
		user: 'root',
		password: 'admin123',
		database: 'book'
	})
}

// 获取随机数组函数
function randomArray(n, l) {
	let rnd = []
	for (let i = 0; i < n; i++) {
		rnd.push(Math.floor(Math.random() * l))
	}
	return rnd
}

function createGuessYouLike(data) {
	const n = parseInt(randomArray(1, 3)) + 1
	data['type'] = n
	switch(n) {
		case 1:
			data['result'] = data.id % 2 === 0 ? '《Executing Magic》' : '《Elements of Robotics》'
			break
		case 2:
			data['result'] = data.id % 2 === 0 ? '《Inproving Psychiatric Care》' : '《Programming Languages》'
			break
		case 3:
			data['result'] = '《Living with Disfigurement》'
			data['prevent'] = data.id % 2 === 0 ? '92%' : '97%'
			break
	}
	return data
}

// 个性化的设置
function createRecommendDate(data) {
	data['readers'] = Math.floor(data.id / 2 * randomArray(1, 100))
	return data
}

function createDate(results, key) {
	const data = results[key]
	return handleData(data)
}

function handleData(data) {
	if (!data.cover.startsWith('http://')) {
		data['cover'] = `${constant.resUrl}/img${data.cover}`
	}
	// 是否选中
	data['selected'] = false
	// 是否私密阅读
	data['private'] = false
	// 是否缓存
	data['cache'] = false
	data['haveRead'] = 0
	return data
}

function createCategoryId(n) {
	const arr = []
	constant.category.forEach((item, index) => {
		arr.push(index + 1)
	})
	const result = []
	for (let i = 0; i < n; i++) {
		// 获取的随机数不能重复
		const ran = Math.floor(Math.random() * (arr.length - i))
		// 获取分类对应的序号
		result.push(arr[ran])
		// 将已经获取的随机数取代掉，用最后一位
		arr[ran] = arr[arr.length - i - 1]
	}
	return result
}

function createCategoryData(data) {
	// 创建分类的id
	const categoryIds = createCategoryId(6)
	const result = []
	categoryIds.forEach(categoryId => {
		// 取出前四本
		const subList = data.filter(item => item.category === categoryId).slice(0, 4)
		subList.map(item => {
			return handleData(item)
		})
		result.push({
			category: categoryId,
			list: subList
		})
	})
	// 返回时，过滤掉分类中书籍小于四本的情况
	return result.filter(item => item.list.length === 4)
}

// 书城首页的API
app.get('/book/home', (req, res) => {
	const conn = connect()
	conn.query('select * from book where cover != \'\'', (err, results) => {
		const length = results.length
		const guessYouLike = []
		// 主页封面的图片
		const banner = constant.resUrl + '/home_banner2.jpg'
		// 推荐图书
		const recommend = []
		// 精选
		const featured = []
		// 随机图书
		const random = []
		const categoryList = createCategoryData(results)
		const categories = [
			{
				category: 1,
				num: 56,
				img1: constant.resUrl + '/cover/cs/A978-3-319-62533-1_CoverFigure.jpg',
				img2: constant.resUrl + '/cover/cs/A978-3-319-89366-2_CoverFigure.jpg'
			},
			{
				category: 2,
				num: 51,
				img1: constant.resUrl + '/cover/ss/A978-3-319-61291-1_CoverFigure.jpg',
				img2: constant.resUrl + '/cover/ss/A978-3-319-69299-9_CoverFigure.jpg'
			},
			{
				category: 3,
				num: 32,
				img1: constant.resUrl + '/cover/eco/A978-3-319-69772-7_CoverFigure.jpg',
				img2: constant.resUrl + '/cover/eco/A978-3-319-76222-7_CoverFigure.jpg'
			},
			{
				category: 4,
				num: 60,
				img1: constant.resUrl + '/cover/edu/A978-981-13-0194-0_CoverFigure.jpg',
				img2: constant.resUrl + '/cover/edu/978-3-319-72170-5_CoverFigure.jpg'
			},
			{
				category: 5,
				num: 23,
				img1: constant.resUrl + '/cover/eng/A978-3-319-39889-1_CoverFigure.jpg',
				img2: constant.resUrl + '/cover/eng/A978-3-319-00026-8_CoverFigure.jpg'
			},
			{
				category: 6,
				num: 42,
				img1: constant.resUrl + '/cover/env/A978-3-319-12039-3_CoverFigure.jpg',
				img2: constant.resUrl + '/cover/env/A978-4-431-54340-4_CoverFigure.jpg'
			},
			{
				category: 7,
				num: 7,
				img1: constant.resUrl + '/cover/geo/A978-3-319-56091-5_CoverFigure.jpg',
				img2: constant.resUrl + '/cover/geo/978-3-319-75593-9_CoverFigure.jpg'
			},
			{
				category: 8,
				num: 18,
				img1: constant.resUrl + '/cover/his/978-3-319-65244-3_CoverFigure.jpg',
				img2: constant.resUrl + '/cover/his/978-3-319-92964-4_CoverFigure.jpg'
			},
			{
				category: 9,
				num: 13,
				img1: constant.resUrl + '/cover/law/2015_Book_ProtectingTheRightsOfPeopleWit.jpeg',
				img2: constant.resUrl + '/cover/law/2016_Book_ReconsideringConstitutionalFor.jpeg'
			},
			{
				category: 10,
				num: 24,
				img1: constant.resUrl + '/cover/ls/A978-3-319-27288-7_CoverFigure.jpg',
				img2: constant.resUrl + '/cover/ls/A978-1-4939-3743-1_CoverFigure.jpg'
			},
			{
				category: 11,
				num: 6,
				img1: constant.resUrl + '/cover/lit/2015_humanities.jpg',
				img2: constant.resUrl + '/cover/lit/A978-3-319-44388-1_CoverFigure_HTML.jpg'
			},
			{
				category: 12,
				num: 14,
				img1: constant.resUrl + '/cover/bio/2016_Book_ATimeForMetabolismAndHormones.jpeg',
				img2: constant.resUrl + '/cover/bio/2017_Book_SnowSportsTraumaAndSafety.jpeg'
			},
			{
				category: 13,
				num: 16,
				img1: constant.resUrl + '/cover/bm/2017_Book_FashionFigures.jpeg',
				img2: constant.resUrl + '/cover/bm/2018_Book_HeterogeneityHighPerformanceCo.jpeg'
			},
			{
				category: 14,
				num: 16,
				img1: constant.resUrl + '/cover/es/2017_Book_AdvancingCultureOfLivingWithLa.jpeg',
				img2: constant.resUrl + '/cover/es/2017_Book_ChinaSGasDevelopmentStrategies.jpeg'
			},
			{
				category: 15,
				num: 2,
				img1: constant.resUrl + '/cover/ms/2018_Book_ProceedingsOfTheScientific-Pra.jpeg',
				img2: constant.resUrl + '/cover/ms/2018_Book_ProceedingsOfTheScientific-Pra.jpeg'
			},
			{
				category: 16,
				num: 9,
				img1: constant.resUrl + '/cover/mat/2016_Book_AdvancesInDiscreteDifferential.jpeg',
				img2: constant.resUrl + '/cover/mat/2016_Book_ComputingCharacterizationsOfDr.jpeg'
			},
			{
				category: 17,
				num: 20,
				img1: constant.resUrl + '/cover/map/2013_Book_TheSouthTexasHealthStatusRevie.jpeg',
				img2: constant.resUrl + '/cover/map/2016_Book_SecondaryAnalysisOfElectronicH.jpeg'
			},
			{
				category: 18,
				num: 16,
				img1: constant.resUrl + '/cover/phi/2015_Book_TheOnlifeManifesto.jpeg',
				img2: constant.resUrl + '/cover/phi/2017_Book_Anti-VivisectionAndTheProfessi.jpeg'
			},
			{
				category: 19,
				num: 10,
				img1: constant.resUrl + '/cover/phy/2016_Book_OpticsInOurTime.jpeg',
				img2: constant.resUrl + '/cover/phy/2017_Book_InterferometryAndSynthesisInRa.jpeg'
			},
			{
				category: 20,
				num: 26,
				img1: constant.resUrl + '/cover/psa/2016_Book_EnvironmentalGovernanceInLatin.jpeg',
				img2: constant.resUrl + '/cover/psa/2017_Book_RisingPowersAndPeacebuilding.jpeg'
			},
			{
				category: 21,
				num: 3,
				img1: constant.resUrl + '/cover/psy/2015_Book_PromotingSocialDialogueInEurop.jpeg',
				img2: constant.resUrl + '/cover/psy/2015_Book_RethinkingInterdisciplinarityA.jpeg'
			},
			{
				category: 22,
				num: 1,
				img1: constant.resUrl + '/cover/sta/2013_Book_ShipAndOffshoreStructureDesign.jpeg',
				img2: constant.resUrl + '/cover/sta/2013_Book_ShipAndOffshoreStructureDesign.jpeg'
			}
		]
		// 根据随机数组生成电子书
		randomArray(9, length).forEach(key => {
			guessYouLike.push(createGuessYouLike(createDate(results, key)))
		})
		// 猜你喜欢
		randomArray(3, length).forEach(key => {
			recommend.push(createRecommendDate(createDate(results, key)))
		})
		// 精选图书
		randomArray(6, length).forEach(key => {
			featured.push(createDate(results, key))
		})
		// 随机推荐
		randomArray(1, length).forEach(key => {
			random.push(createDate(results, key))
		})
		res.json({
			guessYouLike,
			banner, 
			recommend,
			featured,
			categoryList,
			categories,
			random
		})
		// 关闭数据库的链接
		conn.end()
	})
})

// 书本详情的API
app.get('/book/detail', (req, res) => {
	const conn = connect()
	const fileName = req.query.fileName
	const sql = `select * from book where fileName='${fileName}'`
	conn.query(sql, (err, results) => {
		if (err) {
			res.json({
				error_code: 1,
				msg: '电子书详情获取失败！'
			})
		} else {
			if (results && results.length === 0) {
				res.json({
					error_code: 1,
					msg: '电子书详情获取失败！'
				})
			} else {
				const book = handleData(results[0])
				res.json({
					error_code: 0,
					msg: '获取成功！',
					data: book
				})
			}
		}
		// 关闭数据库的链接
		conn.end()
	})
})

// 分类电子书列表API
app.get('/book/list', (req, res) => {
	const conn = connect()
	conn.query('select * from book where cover!=\'\'', 
		(err, results) => {
			if (err) {
				res.json({
					error_code: 1,
					msg: '获取失败！'
				})
			} else {
				results.map(item => handleData(item))
				const data = {}
				constant.category.forEach(categoryText => {
					data[categoryText] = results.filter(item => item.categoryText === categoryText)
				})
				res.json({
					error_code: 0,
					msg: '获取成功！',
					data: data,
					total: results.length // 总数据条数
				})
			}
			conn.end()
		}
	)
})

// 搜索电子书列表API
app.get('/book/flat-list', (req, res) => {
	const conn = connect()
	conn.query('select * from book where cover!=\'\'', 
		(err, results) => {
			if (err) {
				res.json({
					error_code: 1,
					msg: '获取失败！'
				})
			} else {
				results.map(item => handleData(item))
				res.json({
					error_code: 0,
					msg: '获取成功！',
					data: results,
					total: results.length // 总数据条数
				})
			}
			conn.end()
		}
	)
})

// 书架的API
app.get('/book/shelf', (req, res) => {
	res.json({
		bookList: []
	})
})

// 语音合成的api
app.post('/voice', (req, res) => {
	voice(req, res)
})

// 启动服务器
const server = app.listen(3000, () => {
	const host = server.address().address
	const port = server.address().port

	console.log('server is listening at http://%s:%s', host, port)
})
